<template>
		<div class="modal fade" id="selfRoleAuthModal" tabindex="-1" role="dialog" aria-labelledby="roleAuthorizedModel" aria-hidden="true">
			 <div class="modal-dialog">
				<div class="modal-content">	
					<div class="modal-header">
		            	<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
		            	<h4 class="modal-title" id="myModalLabel">角色授权</h4>
          			</div>
          			<div class="table-filter">
					<ul id="roleFunctionTree" class="ztree"></ul>
						<!--<treeNode></treeNode>-->
					</div>
					
					 <div class="modal-footer">
            	<button type="button" class="btn checkedBtn" v-show="btnShow" @click="saveRoleTree()" >确定</button>
            	<!--<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>-->
          		</div>
				</div>
				
			 </div>
			
			 
		</div>
	
</template>
<script>
export default {
  props: ["roleId"],
  data() {
    return {
      btnShow: true,
      nodes: [],
    };
  },
  watch: {
    roleId: {
      handler: function(val, oldVal) {
        this.initTree(val);
      },
      deep: true,
    },
  },
  mounted: function() {
    this.initTree();
  },
  methods: {
    initTree: function(roleId) {
      if (roleId) {
        var _self = this;
        //ztree基础属性设置 /*,chkDisabledInherit: true*/
        var setting = {
          check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: { Y: "ps", N: "s" },
          },
          data: {
            simpleData: { enable: true },
          },
        };
        var url =
          _self.utilHelper.apiUrl + "/api/getAuthorizeRoleListForDevelop";
        var params = {
          roleId: roleId,
          resourcesOrigin: "1",
        };
        var body = {};
        body.params = params;
        _self.$http.post(url, body).then((response) => {
          var resData = response.body.resData;
          var nodeList = resData.nodeList;
          _self.nodes = resData.nodeList;
          $.fn.zTree.init($("#roleFunctionTree"), setting, nodeList);
        });
      }
    },
    saveRoleTree: function() {
      var _self = this;
      var zTree = $.fn.zTree.getZTreeObj("roleFunctionTree");
      var nodes = zTree.getCheckedNodes();
      var aus = "";
      for (var i = 0; i < nodes.length; i++) {
        aus += nodes[i].id + ",";
      }

      if (aus.length > 0) aus = aus.substring(0, aus.length - 1);
      var url = _self.utilHelper.apiUrl + "/api/dp/authorizeRole";
      var params = {
        roleId: _self.roleId,
        resourcesList: [aus],
        merchantId: "",
      };
      var body = {};
      body.params = params;
      _self.btnShow = false;
      _self.$http.post(url, body).then((response) => {
        var body = response.body;
        if (body.result == "1") {
          layer.msg(
            "授权成功!",
            {
              time: 1000,
            },
            function() {
              _self.btnShow = true;
              $("#selfRoleAuthModal").modal("hide");
            }
          );
        } else {
          layer.alert("系统异常!");
          _self.btnShow = true;
        }
      });
    },
  },
};
</script>